package com.cnunicom.modules.tra.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnunicom.common.utils.Constant;
import com.cnunicom.modules.tra.vo.StaffMebHisVO;
import com.cnunicom.modules.tra.vo.UnitMebHisVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;

import com.cnunicom.modules.tra.dao.UnitStaffDao;
import com.cnunicom.modules.tra.entity.UnitStaffEntity;
import com.cnunicom.modules.tra.service.UnitStaffService;


@Service("unitStaffService")
public class UnitStaffServiceImpl extends ServiceImpl<UnitStaffDao, UnitStaffEntity> implements UnitStaffService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        IPage<UnitStaffEntity> page = this.page(
                new Query<UnitStaffEntity>().getPage(params,"create_time",false),
                new QueryWrapper<UnitStaffEntity>()
                    .like(StringUtils.isNotBlank(name),"name",name)
        );

        return new PageUtils(page);
    }

    @Override
    public List<Object> queryStaffMebHisPage(Map<String, Object> params) {
        // 查询条件处理
        StaffMebHisVO vo = voHandler(params);
        IPage<StaffMebHisVO> page = new Query<StaffMebHisVO>()
                .getPage(params, "create_time", false);

        List<StaffMebHisVO> vos = this.baseMapper.queryStaffMebHisPage((Page)params.get(Constant.PAGE), vo);

        Map<String, String> map = totalHandler(vos);
        page.setRecords(vos);

        PageUtils pageUtils = new PageUtils(page);

        List<Object> objects = new ArrayList<>();

        objects.add(pageUtils);
        objects.add(map);

        return objects;
    }

    private Map<String, String> totalHandler(List<StaffMebHisVO> vos) {

       // 会员总数
        Integer totalMeb = vos.stream().map(StaffMebHisVO::getTotalMember).reduce(Integer::sum).orElse(0);

        // 入住房间总数
        Integer totalStay = vos.stream().map(StaffMebHisVO::getTotalStay).reduce(Integer::sum).orElse(0);

        // 总金额
        BigDecimal totalPrice = vos.stream().map(StaffMebHisVO::getTotalPrice).reduce(BigDecimal::add)
                .orElse(BigDecimal.ZERO);

        HashMap<String, String> map = new HashMap<>();
        map.put("totalMeb", totalMeb.toString());
        map.put("totalStay", totalStay.toString());
        map.put("totalPrice", totalPrice.toString());

        return map;
    }

    /**
     * 查询条件处理器，将会使用设计模式修改
     * @param params
     * @return
     */
    private StaffMebHisVO voHandler(Map<String, Object> params) {

        StaffMebHisVO vo = new StaffMebHisVO();

        String name = (String)params.get("name");
        String dept = (String)params.get("dept");

        vo.setDept(dept);
        vo.setName(name);

        return vo;
    }

}
